package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.Setmeal;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //1.调用service处理
        setmealService.saveWithDish(setmealDto);

        //2.响应数据
        return R.success("新增成功！");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        //1.调用service处理 返回Page<SetmealDto>
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);

        //2.响应数据
        return R.success(setmealDtoPage);
    }

    //套餐删除
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {
        //1.调用service处理
        setmealService.deleteByIds(ids);

        //2.响应数据
        return R.success("删除成功！");
    }

    //根据分类id查询套餐列表数据
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //1.调用service处理
        //1.1：创建wrapper对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        //1.2：设置查询条件 分类id和status
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        //1.3：调用service中的list方法查询 传入wrapper对象
        List<Setmeal> list = setmealService.list(wrapper);

        //2.响应数据
        return R.success(list);
    }

    /**
     * 批量奇搜，停用  启用通用套餐
     * 请求网址: http://localhost/setmeal/status/0?ids=1415580119015145474
     * 请求方法: POST
     */
    @PostMapping("/status/{status}")
    public R<String> batchStatus(@PathVariable int status, @RequestParam("ids") List<String> ids) {
        if (ids == null) {
            R.error("请先选着套餐后再执行操作");
        }
        //批量启售 |  停售
        for (String id : ids) {
            //查询该套餐下的菜品是否启售   一个套餐下有多个菜品    1->多 亮哥
            long setmealid = Long.parseLong(id);
            System.out.println(setmealid);
            //根据套餐id，查询分类id
            LambdaQueryWrapper<Setmeal> wrapper1 =
                    new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getId,setmealid);
            Setmeal setmeal = setmealService.getOne(wrapper1);
            Long categoryid = setmeal.getCategoryId();
            //分类下的菜品状态
            LambdaQueryWrapper<Dish> wrapper =
                    new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, categoryid);
            List<Dish> list = dishService.list(wrapper);
            for (Dish dish : list) {
                if (dish.getStatus() == 1) {
                    R.error("该套餐下的菜品【" + dish.getName() + "】还在启售，删除失败");
                }
            }
            //走到这里就是可以直接修改套餐状态了,让其改成status:0 停用 1:启用

            setmeal.setStatus(status);
            setmealService.update(setmeal,wrapper1);
        }

        return status==1?R.success("启售成功"):R.success("停售成功");
    }

}
